package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.interceptor.RequireLongin;
import cn.wolfcode.wolf2w.interceptor.UserParam;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redisService.IStrategyStatisVOService;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.service.impl.StrategyCommentServiceImpl;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.omg.PortableServer.POA;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.StringQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyService strategyService;
   @Autowired
    private IStrategyCommentService commentService;
   @Autowired
   private IStrategyStatisVOService strategyStatisVOService;

    //明细
    @GetMapping("/detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        //攻略阅读数+1
        strategyStatisVOService.viewnumIncrease(id,1);
        return JsonResult.success(strategy);
    }
    /*@GetMapping("/themes")
    public Object list(String destId){
        return JsonResult.success(strategyThemeService.getAll());
   }*/
    @GetMapping("/query")
    public  Object query(StrategyQuery qo){
       return JsonResult.success(strategyService.query(qo));
    }
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }
    @RequireLongin//必须登录后
    @PostMapping("/addComment")
    //添加一条评论
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        BeanUtils.copyProperties(userInfo,comment);
        comment.setUserId(userInfo.getId());
        commentService.save(comment);
        //评论数+1 带上攻略id 和num(+1)
        strategyStatisVOService.replynumIncrease(comment.getStrategyId(),1);
        return JsonResult.success();
    }
    //页面显示评论
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){

        return JsonResult.success(commentService.query(qo));
    }
    //阅读数
    @RequireLongin
    @PostMapping("/commentThumb")
    //点赞
    public Object commentThumb(String cid, @UserParam UserInfo userInfo){
        commentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }
    //阅读数回显
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVOService.findBySid(sid));
    }

    @RequireLongin
    @PostMapping("/favor")
    //攻略收藏
    public Object favor(String sid, @UserParam UserInfo userInfo){
        boolean bn= strategyStatisVOService.favor(sid,userInfo.getId());
        return JsonResult.success(bn);
    }
    //顶
    @RequireLongin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean thumbup = strategyStatisVOService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(thumbup);
    }
}
